from urllib import parse,request
import zlib
import json
import os

class Get:
	def __init__(self): pass
	def save(self,data):
		path=os.getcwd()
		f1 = open(path+'//response.txt', 'w',encoding='utf8')
		try:
			if data.strip()!='':
				f1.write(data)
			else:
				print('sql数据为空')
		finally:
			f1.close()
	def httpGet(self,sql,db_name):
		textmod = {}
		textmod['id'] = '7700'
		textmod['hiveSql'] = sql+'\n\r'
		textmod['db_name'] = db_name
		textmod['select_db'] = '0'
		textmod['cas_info[0]']='cai.tian'
		textmod['cas_info[1]']='cai.tian@vipshop.com'
		textmod['cas_info[2]']='20161101103559'
		textmod = parse.urlencode(textmod)
		#输出内容:user=admin&password=admin
		header_dict={}
		header_dict['Accept']='application/json, text/javascript, */*; q=0.01'
		header_dict['Accept-Encoding']='gzip, deflate'
		header_dict['Accept-Language']='zh-CN,zh;q=0.9'
		header_dict['Connection']='keep-alive'
		header_dict['Cookie']='P0001=1; sn=cai.tian; email=cai.tian%40vipshop.com; cn=20161101103559; uid=20161101103559; laravel_session=eyJpdiI6InE5ekNaNCtXVkRXNUpDSDZBZVc4ekE9PSIsInZhbHVlIjoiNjFHVTZmQXZ3cDlBNlo3aGNwSHpTWlBJaHFnV3hGSkJDTEQ0WStZU0NMZ1E5RCtpd1VxMjQwZUI1dVwveURwYndlNjdaVm0zSWowZGx0R1Yrcm14bTl3PT0iLCJtYWMiOiI0NzY5YmZlM2YzY2NhNWNjMmE4NGQzNmUzOGZkN2Q5MTUxODYzMWQ0YzhiMmI0MWU3MGJkNmJhYjQ4ZTc0Y2E5In0%3D'
		header_dict['Host']='dataqjr.tools.vipshop.com'
		header_dict['Referer']='http://dataqjr.tools.vipshop.com/html_hive/hiveData.html?uid=undefined&num=0.5408550624740283'
		header_dict['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'
		header_dict['X-Requested-With']='XMLHttpRequest'
			
		url='http://dataqjr.tools.vipshop.com/query/mysql'
		print("%s?%s"%(url, textmod))
		req = request.Request("%s?%s"%(url, textmod),headers=header_dict) #省略data参数即为get方式访问

		with request.urlopen(req) as f:
		    data = f.read()
		    print('Status:', f.status, f.reason)
		    for k, v in f.getheaders():
		        print('%s: %s' % (k, v))
		    decompressed_data = zlib.decompress(data ,zlib.MAX_WBITS|32)
		    #输出内容(python3默认获取到的是16进制'bytes'类型数据 Unicode编码，如果如需可读输出则需decode解码成对应编码):b'\xe7\x99\xbb\xe5\xbd\x95\xe6\x88\x90\xe5\x8a\x9f'
		    josns=str(decompressed_data.decode(encoding='utf-8'))
		    self.save(josns)
		    return josns

#print(repr(decompressed_data.decode(encoding='utf-8')))

